// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/stmts/load_data_stmt.h>
#include <cantor/transform/dmls/fields_clause.h>
#include <cantor/transform/dmls/lines_clause.h>
#include <cantor/transform/exprs/range_var.h>
#include <cantor/transform/clause/table_name.h>
#include <cantor/transform/dmls/assignment.h>

#include "cantor/transform/clause/column_name.h"

namespace cantor::transform {

    void TransLoadDataStmt::resolve_ast(const cantor::parser::LoadDataStmt*node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        load_data_stmt.set_ignore_lines(node->ignore_lines);
        load_data_stmt.set_is_local(node->is_local);
        load_data_stmt.set_path(node->path.to_string());
        load_data_stmt.set_char_set(node->char_set.to_string());

        if (node->fields_info) {
            status = TransFieldsClause::resolve_to(node->fields_info, load_data_stmt.mutable_fields_info());
            if (!status.ok()) {
                return;
            }
        }

        if (node->lines_info) {
            status = TransLinesClause::resolve_to(node->lines_info, load_data_stmt.mutable_lines_info());
            if (!status.ok()) {
                return;
            }
        }
        if (node->table_name) {
            status = TransTableName::resolve_to(node->table_name, load_data_stmt.mutable_table_name());
            if (!status.ok()) {
                return;
            }
        }
        load_data_stmt.set_on_duplicate_handle(node->on_duplicate_handle);

        auto cols = load_data_stmt.mutable_columns();
        cols->Reserve(node->columns.size());
        for (size_t i = 0; i < node->columns.size(); ++i) {
            status = TransColumnName::resolve_to(node->columns[i], cols->Add());
            if (!status.ok()) {
                return;
            }
        }

        auto ls = load_data_stmt.mutable_set_list();
        ls->Reserve(node->set_list.size());
        for (size_t i = 0; i < node->set_list.size(); ++i) {
            status = TransAssignment::resolve_to(node->set_list[i], ls->Add());
            if (!status.ok()) {
                return;
            }
        }
        set_proto_location(node, load_data_stmt.mutable_location());
        set_proto_status(load_data_stmt.mutable_status());
    }

    const turbo::Status &TransLoadDataStmt::check() {
        if (!status.ok()) {
            return status;
        }
        if (!load_data_stmt.has_location()) {
            make_missing_required_field("location");
        }
        return status;
    }

    turbo::Status TransLoadDataStmt::resolve_to(turbo::Nonnull<const cantor::parser::LoadDataStmt*> arg, turbo::Nonnull<pb::ast::LoadDataStmt *>out) {
        TransLoadDataStmt trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.load_data_stmt);
        return status;
    }
}  // namespace cantor::transform
